//
//  leaveViewController.m
//  GaKan
//
//  Created by hztech on 2017/12/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "leaveViewController.h"
#import "JSDropDownMenu.h"
#import "WYBirthdayPickerView.h"
#import "JTCalendar.h"
#define CellColor [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0]

@interface leaveViewController ()<UITextViewDelegate,JSDropDownMenuDataSource,JSDropDownMenuDelegate>
//各个控件
@property(strong,nonatomic)UITextView *textView;
@property(strong,nonatomic)UILabel *count;
//下拉菜单
@property(strong,nonatomic)JSDropDownMenu *menu;
@property(strong,nonatomic)UIButton *startbtn;
@property(strong,nonatomic)UIButton *endbtn;
@property(strong,nonatomic)UIButton *makesure;
@property(strong,nonatomic)UILabel *lab1;
@property(strong,nonatomic)UILabel *lab2;

@end

@implementation leaveViewController
//全局变量
{
    NSMutableArray *_data2;
    NSInteger _currentData2Index;
    NSDate *startDate;
    NSDate *endDate;
    NSString *type;
}
//重写init
-(instancetype)init{
    self = [super init];
    if (self)
    {
        [self setToptittle:@"请假"];
        [self setTopTittleColor:[UIColor whiteColor]];
        [self setTopBgColor:headColor];
        [self setBackBtnImage:[UIImage imageNamed:@"wo_merchandise_detail_back@3x"]];
        [self setBackBtnHide:NO];
    }
    return self;
}
//主函数---ui视图搭建
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *typelab = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 60, 40)];
    typelab.text = @"类别:";
    [self.view addSubview:typelab];
    
    [self.view addSubview:self.textView];
    _data2 = [NSMutableArray arrayWithObjects:@"事假", @"病假", @"婚假", @"孕假", @"产假", nil];
    type=@"事假";
    [self.view addSubview:self.menu];
    UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.textView.frame)+20, 100, 40)];
    timeLab.text = @"请假时间:";
    [self.view addSubview:timeLab];
    [self.view addSubview:self.lab1];
    [self.view addSubview:self.lab2];
    [self.view addSubview:self.startbtn];
    [self.view addSubview:self.endbtn];
    [self.view addSubview:self.makesure];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.textView.frame)+60, ScreenWidth, 40)];
    lab.text = @"至";
    lab.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:lab];
}

#pragma mark  -  懒加载
//确认按钮
-(UIButton*)makesure{
    if (!_makesure) {
        _makesure = [[UIButton alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.startbtn.frame)+40, ScreenWidth-60, 40)];
        [_makesure setTitle:@"确认" forState:UIControlStateNormal];
        _makesure.backgroundColor = [UIColor orangeColor];
        _makesure.titleLabel.font = [UIFont systemFontOfSize: 17.0];
        
        _makesure.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [_makesure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_makesure addTarget:self action:@selector(btnSure) forControlEvents:UIControlEventTouchDown];
        _makesure.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _makesure;
}
//确认函数
-(void)btnSure{
    if ([_lab1.text isEqual:@"起始时间"]||[_lab2.text isEqual:@"截止时间"]) {
        DSToast *toast=[[DSToast alloc]initWithText:@"请选择时间"];
        [toast showInView:self.view];
        return;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    startDate = [formatter dateFromString:_lab1.text];
    endDate = [formatter dateFromString:_lab2.text];
    JTCalendarManager *calendarManager = [JTCalendarManager new];
    if (![calendarManager.dateHelper date:startDate isEqualOrBefore:endDate]) {
        DSToast *toast=[[DSToast alloc]initWithText:@"请选择正确的时间"];
        [toast showInView:self.view];
        return;
    }
    [self submit];
    
}
//提交函数
-(void)submit{
    BmobObject *obj = [[BmobObject alloc] initWithClassName:@"leave"];
    NSString *str=[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
    [obj setObject:str forKey:@"user_id"];
    
    [obj setObject:startDate forKey:@"leave_start"];
    [obj setObject:endDate forKey:@"leave_end"];
    [obj setObject:type forKey:@"leave_type"];
    [obj setObject:@"0" forKey:@"leave_isOK"];
    [obj setObject:_textView.text forKey:@"leave_reason"];

    
    [obj saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        NSLog(@"%@",error);
        if (!error) {
            NSLog(@"提交成功");
            [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];

        }else{
            DSToast *toast=[[DSToast alloc]initWithText:[NSString stringWithFormat:@"%@",error]];
            [toast showInView:self.view];
        }
        
    }];
}
//起始时间
-(UILabel*)lab1{
    if (!_lab1) {
        _lab1=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.textView.frame)+60, (ScreenWidth-60)/2, 40)];
        _lab1.text=@"起始时间";
        _lab1.font=[UIFont systemFontOfSize:17];
        _lab1.textAlignment=NSTextAlignmentCenter;
        _lab1.textColor=[UIColor blackColor];
    }
    return _lab1;
}
//截止时间
-(UILabel*)lab2{
    if (!_lab2) {
        _lab2=[[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth-60)/2+40, CGRectGetMaxY(self.textView.frame)+60, (ScreenWidth-60)/2, 40)];
        _lab2.text=@"截止时间";
        _lab2.font=[UIFont systemFontOfSize:17];
        _lab2.textAlignment=NSTextAlignmentCenter;
        _lab2.textColor=[UIColor blackColor];
    }
    return _lab2;
}
//起始时间按钮
-(UIButton*)startbtn{
    if (!_startbtn) {
        _startbtn = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.textView.frame)+60, (ScreenWidth-60)/2, 40)];
        //[_startbtn setTitle:@"起始时间" forState:UIControlStateNormal];
        _startbtn.backgroundColor = [UIColor clearColor];
        //_startbtn.titleLabel.font = [UIFont systemFontOfSize: 17.0];
        _startbtn.layer.cornerRadius = 2.0;
        [_startbtn.layer setBorderWidth:1.0];
        _startbtn.layer.borderColor = [UIColor grayColor].CGColor;
        _startbtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [_startbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_startbtn addTarget:self action:@selector(btn1) forControlEvents:UIControlEventTouchDown];
        _startbtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _startbtn;
}
//起始时间回调函数
-(void)btn1{
    WYBirthdayPickerView *birthdayPickerView1 = [[WYBirthdayPickerView alloc] initWithInitialDate:@""];
    
    birthdayPickerView1.confirmBlock = ^(NSString *selectedDate) {
        self.lab1.text = selectedDate;
        //self.startbtn.titleLabel.text = selectedDate;
        NSLog(@"%@",selectedDate);
    };
    
    [self.view addSubview:birthdayPickerView1];
}
//截止时间按钮
-(UIButton*)endbtn{
    if (!_endbtn) {
        _endbtn = [[UIButton alloc]initWithFrame:CGRectMake((ScreenWidth-60)/2+40, CGRectGetMaxY(self.textView.frame)+60, (ScreenWidth-60)/2, 40)];
        //[_endbtn setTitle:@"截止时间" forState:UIControlStateNormal];
        _endbtn.backgroundColor = [UIColor clearColor];
        //_endbtn.titleLabel.font = [UIFont systemFontOfSize: 17.0];
        _endbtn.layer.cornerRadius = 2.0;
        [_endbtn.layer setBorderWidth:1.0];
        _endbtn.layer.borderColor = [UIColor grayColor].CGColor;
        _endbtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [_endbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_endbtn addTarget:self action:@selector(btn2) forControlEvents:UIControlEventTouchDown];
    }
    return _endbtn;
}
//截止时间回调函数
-(void)btn2{
    WYBirthdayPickerView *birthdayPickerView2 = [[WYBirthdayPickerView alloc] initWithInitialDate:@""];
    
    birthdayPickerView2.confirmBlock = ^(NSString *selectedDate) {
        self.lab2.text = selectedDate;
        //self.endbtn.titleLabel.text = selectedDate;
    };
    
    [self.view addSubview:birthdayPickerView2];
}
//下拉菜单
-(JSDropDownMenu*)menu{
    if (!_menu) {
        _menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(ScreenWidth/3, 80) andHeight:40];
        _menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
        _menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
        _menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
        _menu.dataSource = self;
        _menu.delegate = self;
        
    }
    return _menu;
}
//以下为控件代理协议
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 1;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    return YES;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    return _currentData2Index;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    return _data2.count;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    return _data2[0];
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    return _data2[indexPath.row];

}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    _currentData2Index = indexPath.row;
    type=_data2[indexPath.row];
}
//输入框
-(UITextView *)textView{
    if (!_textView) {
        // 初始化输入框并设置位置和大小
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 140, [UIScreen mainScreen].bounds.size.width - 20, 150)];
        //https://www.jianshu.com/p/14385b1db4f3
        // 设置预设文本
        _textView.text = @"请写下你的请假理由";
        // 设置文本字体
        _textView.font = [UIFont fontWithName:@"Arial" size:16.5f];
        // 设置文本颜色
        _textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];
        // 设置文本框背景颜色
        _textView.backgroundColor = [UIColor whiteColor];
        // 设置文本对齐方式
        _textView.textAlignment = NSTextAlignmentLeft;
        // 设置自动纠错方式
        _textView.autocorrectionType = UITextAutocorrectionTypeNo;
        
        //外框
        _textView.layer.borderColor = [UIColor orangeColor ].CGColor;
        _textView.layer.borderWidth = 1;
        _textView.layer.cornerRadius =5;
        
        // 设置自动大写方式
        _textView.autocapitalizationType = UITextAutocapitalizationTypeNone;

        // 设置键盘的样式
        _textView.keyboardType = UIKeyboardTypeDefault;

        // 设置return键样式
        _textView.returnKeyType = UIReturnKeyDefault;
        
        // 设置是否可以拖动
        _textView.scrollEnabled = YES;
        // 设置代理
        _textView.delegate = self;
        //自适应高度
        _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        // 自定义文本框字数统计
        _count = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100, CGRectGetMaxY(_textView.frame) +5, 60, 20)];
        _count.text = @"0/100";
        _count.textAlignment = NSTextAlignmentRight;
        _count.font = [UIFont fontWithName:@"Arial" size:15.0f];
        _count.backgroundColor = [UIColor clearColor];
        _count.textColor = [UIColor redColor];
        _count.enabled = NO;
        
        [self.view addSubview:_count];

    }
    return _textView;
}
/**
 将要开始编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
    
}
/**
 开始编辑
 @param textView textView
 */
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请写下你的请假理由"]) {
        
        textView.text = @"";
        
    }
}

/**
 将要结束编辑
 
 @param textView textView
 
 @return BOOL
 */
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    
    return YES;
    
}

/**
 结束编辑
 
 @param textView textView
 */
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1) {
        textView.text = @"您的意见是我们前进的最大动力，谢谢！";
    }
    
    
}


/**
 内容将要发生改变编辑 限制输入文本长度 监听TextView 点击了ReturnKey 按钮
 
 @param textView textView
 @param range    范围
 @param text     text
 
 @return BOOL
 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location < 100)
    {
        return  YES;
        
    } else  if ([textView.text isEqualToString:@"\n"]) {
        
        //这里写按了ReturnKey 按钮后的代码
        return NO;
    }
    
    if (textView.text.length == 100) {
        
        return NO;
    }
    
    return YES;
    
}


/**
 内容发生改变编辑 自定义文本框placeholder
 有时候我们要控件自适应输入的文本的内容的高度，只要在textViewDidChange的代理方法中加入调整控件大小的代理即可
 @param textView textView
 */
- (void)textViewDidChange:(UITextView *)textView
{
    _count.text = [NSString stringWithFormat:@"%lu/100", (unsigned long)textView.text.length  ];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
